// **************************自行接收***************************//
/*孙瑞豪 自行接收方法封装js*/
// 导出Excel
function download() {
    var url = "/correctedPerson/exportExcel";
    window.open(url);
}

// 模糊查询分页列表
var getCorrectedPersonList = function(curr, cur) {
    $.ajax({
        type: "get",
        dataType: "json",
        url: "/correctedPerson/querylike",
        data: {
            pageNum: curr || 1,
            pageSize: cur || 10,
            cunit: $("#cunit").val(),
            cname: $("#cname").val(),
            cdate: $("#cdate").val()
        },
        // 显示分页
        success: function(msg) {
            app.result = msg.result;
            $('#pagediv').pagination({
                total: msg.totalNum,
                buttons: [{
                    text: '删除所选',
                    iconCls: 'icon-cut',
                    handler: function() {
                        alldelete('correctedPerson');
                    }
                },
                    {
                        text: '导出所选',
                        iconCls: 'icon-save',
                        handler: function() {
                            exportchoice('correctedPerson');
                        }
                    }],
                pageSize: cur,
                pageNumber: curr,
                pageList: [1, 5, 10, 15, 20],
                beforePageText: '第',
                afterPageText: '页    共 {pages} 页',
                layout: ['list', 'sep', 'first', 'prev', 'links', 'next', 'last', 'sep', 'refresh', 'manual'],
                displayMsg: '当前显示第 {from} 至  第 {to} 条记录   共 {total} 条记录',
                onSelectPage: function(pageNumber, pageSize) {
                    getCorrectedPersonList(pageNumber, pageSize);
                }
            });
        }
    });
}

// 模糊查询违规违纪记录
var queryCorrectedPerson = function() {
    $.ajax({
        type: "get",
        dataType: "json",
        url: "/correctedPerson/querylike",
        data: {
            pageNum: 1,
            pageSize: 10,
        },
        success: function(msg) {
            app.result = msg.result;
            $('#pagediv').pagination({
                total: msg.totalNum,
                buttons: [{
                    text: '删除所选',
                    iconCls: 'icon-cut',
                    handler: function() {
                        alldelete('correctedPerson');
                    }
                },
                    {
                        text: '导出所选',
                        iconCls: 'icon-save',
                        handler: function() {
                            exportchoice('correctedPerson');
                        }
                    }],
                pageSize: 10,
                pageList: [1, 5, 10, 15, 20],
                beforePageText: '第',
                afterPageText: '页    共 {pages} 页',
                layout: ['list', 'sep', 'first', 'prev', 'links', 'next', 'last', 'sep', 'refresh', 'manual'],
                displayMsg: '当前显示第 {from} 至  第 {to} 条记录   共 {total} 条记录',
            });
            getCorrectedPersonList();
        }
    });
}

// 增加入娇人员信息
var addEvent = function() {
    $('#dlg').dialog({
        title: '增加---违规违纪记录',
        width: 600,
        height: 360,
        closed: false,
        cache: false,
        href: '/correctedPerson/preAdd?sort=correctedPerson',
        modal: true,
        buttons: [{
            text: '保存',
            iconCls: 'icon-ok',
            handler: function() {
                var formData = new FormData($("#form3")[0]);
                $.ajax({
                    url: "/correctedPerson/addAll",
                    type: "get",
                    dataType: "json",
                    data: formData,
                    success: function(result) {
                        $.messager.alert({
                            title: '消息',
                            msg: '保存成功!',
                            icon: 'info'
                        });
                        getCorrectedPersonList();
                        $('#dlg').dialog('close');
                    }
                });
            }
        },
            {
                text: '关闭',
                handler: function() {
                    $('#dlg').dialog('close');
                }
            }]
    });
}

// 修改待入娇人员信息
var editEvent = function(id) {
    $('#dlg2').dialog({
        title: '修改---违规违纪记录',
        width: 600,
        height: 360,
        closed: false,
        cache: false,
        href: '/correctedPerson/getAllEdit?sort=edit&id=' + id,
        modal: true,
        buttons: [{
            text: '保存',
            iconCls: 'icon-ok',
            handler: function() {
                var formData = new FormData($("#form2")[0]);
                $.ajax({
                    url: "/correctedPerson/editPer",
                    type: "get",
                    dataType: "json",
                    data: formData,
                    success: function(result) {
                        $.messager.alert({
                            title: '消息',
                            msg: '修改成功!',
                            icon: 'info'
                        });
                        getCorrectedPersonList();
                        $('#dlg2').dialog('close');
                    }
                });
            }
        },
            {
                text: '关闭',
                handler: function() {
                    $('#dlg2').dialog('close');
                }
            }]
    });

}

// 查看违规违纪记录详情
var checkRuleBreak = function(id) {
    $('#dlg1').dialog({
        title: '查看---违规违纪记录详情',
        width: 600,
        height: 360,
        closed: false,
        cache: false,
        href: '/correctedPerson/getAll?sort=check&id=' + id,
        modal: true,
        buttons: [{
            text: '关闭',
            handler: function() {
                $('#dlg1').dialog('close');
            }
        }]
    });

}

// 删除入娇人员信息
var delRuleBreak = function(id) {
    $.messager.confirm('提示框', '你确定要删除吗?',
        function(res) {
            if (res) {
                $.ajax({
                    url: "/correctedPerson/delAll",
                    type: "get",
                    dataType: "json",
                    data: {
                        id: id
                    },
                    success: function(result) {
                        $.messager.alert({
                            title: '消息',
                            msg: '删除成功!',
                            icon: 'info'
                        });
                        getCorrectedPersonList(1,10);
                    }
                });
            } else {
                // 取消删除
            }
        })
}

// 批量删除所选
function alldelete(sort) {
    //获取所有name为check的复选框的数组
    var id = document.getElementsByName("check");
    //定义一个数组
    var ids = [];
    //定义一个变量,记录选中的复选框个数
    var i = 0;
    //遍历数组id
    for (k in id) {
        //取选中的复选框的value属性,赋值给ids数组
        if (id[k].checked) {
            i++;
            ids.push(id[k].value);
        }
    }
    //如果选中的复选框数大于0,提示删除
    if (i > 0) {
        $.messager.confirm('提示框', '你确定要删除' + i + '条数据吗?',
            function(res) {
                if (res) {
                    $.ajax({
                        url: "/" + sort + "/getDeleteIds",
                        traditional: true,
                        type: "get",
                        dataType: "text",
                        data: {
                            id: ids
                        },
                        success: function(result) {
                            $.messager.alert({
                                title: '消息',
                                msg: '删除' + i + '条数据成功!',
                                icon: 'info'
                            });
                            $("input[name='check']").prop("checked", false);
                            $("input[name='allcheck']").prop("checked", false);
                            if (sort == "monthtest") {
                                getMonthTestList(1,10);
                            } else if (sort == "activist") {
                                getActivistList(1,10);
                            } else if (sort == "bonuspoints") {
                                getBonusPointsList(1,10);
                            } else if (sort == "rulebreak") {
                                getRuleBreakList(1,10);
                            } else {
                                getRewordPunishList(1,10);
                            }
                        }
                    })
                } else {
                    // 取消删除
                }
            })
        //如果选中的复选框数小于0,提示未选中复选框
    } else {
        $.messager.alert({
            title: '消息',
            msg: '请至少选择一项!',
            icon: 'info'
        });
    }
}

// 批量导出所选
function exportchoice(sort) {
    var id = document.getElementsByName("check");
    var ids = [];
    var i = 0;
    for (k in id) {
        if (id[k].checked) {
            i++;
            ids.push(id[k].value);
        }
    }
    if (i > 0) {
        $.messager.confirm('提示框', '你确定要导出' + i + '条数据吗?',
            function(res) {
                if (res) {
                    var url = "/"+sort+"/exportExcel?id=" + ids;
                    window.open(url);
                    $("input[name='check']").prop("checked", false);
                    $("input[name='allcheck']").prop("checked", false);
                } else {
                    // 取消删除
                }
            })
    } else {
        $.messager.alert({
            title: '消息',
            msg: '请至少选择一项!',
            icon: 'info'
        });
    }
}
